package gwtmodule1.client.gui.shared;

import gwtmodule1.client.gui.utils.DekoTable;
import gwtmodule1.client.gui.utils.FrameClient;
import gwtmodule1.client.gui.utils.FrameProvider;
import gwtmodule1.client.gui.utils.SwitchPage;

import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Hyperlink;

public class OrderTable extends DekoTable {

	private static final int COL_ID = 0;

	private static final int COL_DATE = 1;

	private static final int COL_NAME = 2;

	private static final int COL_ITEMS = 3;

	private static final int COL_VALUE = 4;

	private static final int COL_STATUS = 5;

	private static final int COL_DETAILS = 6;

	private static final int COL_EDIT_ITEMS = 7;

	private static final int COL_EDIT_SHIP = 8;

	private FrameProvider provider;

	private FrameClient details;

	private FrameClient editItems = null;

	private FrameClient editShip = null;

	public OrderTable(FrameProvider provider, FrameClient details) {
		super();
		this.provider = provider;
		this.details = details;
		setText(0, COL_ID, "ID");
		setText(0, COL_DATE, "Date");
		setText(0, COL_NAME, "Name");
		setText(0, COL_ITEMS, "Items");
		setText(0, COL_VALUE, "Value");
		setText(0, COL_STATUS, "Status");
		setText(0, COL_DETAILS, "");
	}

	public void setAdminLinks(FrameClient editItems, FrameClient editShip) {
		getFlexCellFormatter().setColSpan(0, COL_DETAILS, 3);
		this.editItems = editItems;
		this.editShip = editShip;
	}

	public void addItem(int id, String date, String name, int items,
			float value, String status) {
		Hyperlink details = new Hyperlink("details", "OrderDetails" + id);
		details.addClickListener(new SwitchPage(provider, this.details));
		int i = getRowCount();
		setText(i, COL_ID, id + "");
		setText(i, COL_DATE, date);
		setText(i, COL_NAME, name);
		setText(i, COL_ITEMS, items + "");
		setText(i, COL_VALUE, value + " USD");
		setText(i, COL_STATUS, status);
		setWidget(i, COL_DETAILS, details);
		getCellFormatter().setHorizontalAlignment(i, COL_ITEMS,
				HasHorizontalAlignment.ALIGN_RIGHT);
		getCellFormatter().setHorizontalAlignment(i, COL_VALUE,
				HasHorizontalAlignment.ALIGN_RIGHT);
		if (editItems != null & editShip != null)
			addAdminLinks(i, id);
	}

	private void addAdminLinks(int i, int id) {
		Hyperlink items = new Hyperlink("items", "OrderItems" + id);
		items.addClickListener(new SwitchPage(provider, editItems));
		setWidget(i, COL_EDIT_ITEMS, items);
		Hyperlink ship = new Hyperlink("edit", "OrderShip" + id);
		ship.addClickListener(new SwitchPage(provider, editShip));
		setWidget(i, COL_EDIT_SHIP, ship);
	}

}